var EditArticle = {};

EditArticle.source = function(ft){
	if(liObj){
		$("#_start").val(0);
	
		EditArticle.createDialog(ft);
		
		$("#source_form").ajaxSubmit({
			url:'/CMS/main/resouceList',
			type:'post',
			dataType:'json',
			success:function(data){
				page(data.count,$("#_limit").val(),1,"_pagePanel","gotoPageSource",true,null,null,false);
				//加载添加时间
				var lines = '<option value="">全部</option>';
				$.each(data.addTime,function(j,m){
					if(m != null)
						lines += '<option value="'+m+'">'+m+'</option>';
				});
				
				$("#faddTimeQuery").html(lines);
				
				bindGridSource(data);
			},	
			error:function(msg){
				$.growlUI('数据加载出错！');
			}
		});
	}else{
		$.growlUI('请选择栏目');
	}
};

EditArticle.selectAddTimeQuery = function(type){
	$("#_start").val(0);
	
	var datas = {'faddtime':$("#faddTimeQuery").val()==""?null:$("#faddTimeQuery").val(),'fname':$("#searchSourceName").val()==""?null:$("#searchSourceName").val()};
	
	$("#source_form").ajaxSubmit({
		url:'/CMS/main/resouceList',
		type:'post',
		dataType:'json',
		data:datas,
		success:function(data){
			page(data.count,$("#_limit").val(),1,"_pagePanel","gotoPageSource",true,null,null,false);
			bindGridSource(data);
		},
		error:function(msg){
			$.growlUI('数据加载出错！');
		}
	});
};
//创建弹出层
EditArticle.createDialog = function(ftitle){
	var html = '<div style="overflow:auto;overflow-y:auto;overflow-x:hidden;">\
				<div id="time_source"><span style="float:left;">添加时间：</span><select id="faddTimeQuery"  style="width:180px;font-size:13px;"></select>\
				<span><span>资源名称：</span><input type="text" id="searchSourceName" /></span><input type="button" value="    搜   索     " onclick="EditArticle.selectAddTimeQuery()"/></div><br>\
				<div id="source_div" style="padding:10px;"></div>\
				<div class="page_control">\
				<span class="f_l" id="_totalCount"></span>\
				<span class="f_r" id ="_pagePanel"></span>\
				</div>\
				</div>';
		var sourcekdialog = KindEditor.dialog({
			width : 700,
			height: 600,
			title : ftitle,
			body : html,
			shadowMode:true,
			closeBtn : {
				name : '关闭',
				click : function(e) {
					sourcekdialog.remove();
				}
			},
			yesBtn : {
				name : '确定',
				click : function(e) {
					var obj = $("input[type='radio'][name='sourceId']:checked");
					if(obj.size() > 0){
						var _data = {'fsiteid': $("#select_web").val(),'fcolumnid':liObj.value,'fresourceid':obj.val(),'operType':1};
						VP.update(_data);
					}
				}
			},
			noBtn : {
				name : '取消',
				click : function(e) {
					sourcekdialog.remove();
				}
			}
		});
};
function gotoPageSource(start){
	$("#_start").val(start);
	var datas = {'faddtime':$("#faddTimeQuery").val()==""?null:$("#faddTimeQuery").val(),'fname':$("#searchSourceName").val()==""?null:$("#searchSourceName").val()};

	$("#source_form").ajaxSubmit({
		url:'/CMS/main/resouceList',
		type:'post',
		dataType:'json',
		data:datas,
		success:function(data){
			bindGridSource(data);
		},
		error:function(msg){
			$.growlUI('数据加载出错！');
		}
	});
}
function bindGridSource(data){
	$("#_totalCount").html('总共：'+data.count+'条');
	
	var sourType = $("#type").val();
	
	var html = '<table  width="100%" border="0">';
	if(data.source != ''){
		var tem = 0;
		var wH,hH,zoom;
		
		$.each(data.source,function(i,u){
			zoom = Math.max( Math.max(u.fwidth/140,1) , Math.max(u.fheight/100,1) );
			wH   = u.fwidth / zoom;
			hH   = u.fheight / zoom;
			
			var line = '';
			if(tem == 0){
				line = '<tr style="height:130px;">';
			}
				line +='<td width="33%"  style="text-align:center;"><img title="' +u.fname+ '" id="fresourceurl'+u.id+'" ';
			if(sourType == 1){
				line += 'src="' + u.fresourceurl + '" ';
			}else if(sourType == 2){
				line += 'src="' + u.fbillurl + '" dataUrl = "' + u.fresourceurl + '" sendMode="'+ u.fdefinition +'" mediatype="'+ u.fmediatype +'" duration="'+ u.fduration +'"';
			}	
				line += ' onclick="document.getElementById(\'sourceId'+u.id+'\').checked=true;"  onerror="this.src = \'/CMS/images/imgprew.png\'" style="width:' +  (wH > 0?wH:100) + 'px;height:' + (hH > 0?hH:100) + 'px;cursor:pointer;"/>'
						+'<br><span  style="font-size:14px;width:150px;color:#688187;word-break:break-all;overflow: hidden;white-space: nowrap; text-overflow: ellipsis;display:inline-block;" id="title_'+ u.id +'">'+u.fname+'</span>'
						+'<br><input type="radio" style="margin-left:30%;" id="sourceId'+u.id+'" name="sourceId" value="'+u.id+'" /></td>';
				tem ++;	
				
			if(tem == 3){
				line += '</tr>';
				tem = 0;
			}	
			html += line;	
		});
		
	}else{
		html += '<tr><td colspan="3" style="text-align:center;">无数据</td></tr>';
	}
	html += '</table>';
//	var htmlStr =  html;
//	$.growlUI(htmlStr);
//	if($("#type").val() == 1){///图片加载器
//		var imgtab = '<div style="width:100%;height:auto !important;min-height:30px;border:1px solid #ffffff;">'
//			+'<div id="div_tab1" onclick="DivTab.clickOn(1,2)" class="div_li_style_on">图片库</div><div id="div_tab2" onclick="DivTab.clickOn(2,1)" class="div_li_style">上传图片</div>'
//			+'<div id="div_tab_sorce1" style="width:100%;border:1px solid #ffffff;border-top-color:#ccc;margin-top:20px;">'+htmlStr+'</div>'
//			+'<div id="div_tab_sorce2" style="width:100%;border:1px solid #ffffff;border-top-color:#ccc;margin-top:20px;display:none;">'
//			+'<div style="margin-top:20px;"><form id="img_upload_form" action="article_uploadimage" method="post" enctype="multipart/form-data">'
//			+'选择图片：<input type="file" name="file" style="font-size:13px;" onchange ="ImgUpload.validataFileType(this)"/>'
//			+'<input type="button" value="上传" id="uploadimg" onclick="ImgUpload.imgupload()" style="font-size:13px;width:50px;"/>'
//			+'(支持格式：.gif.jpg.jpeg.png)</form></div>'
//			+'<div style="clear:both;"></div></div>'
//			+'</div>';
//		$("#source_div").html(imgtab);
//	}else{
		$("#source_div").html(html);
//	}
}
var ImgUpload ={
		//上传图片验证
		validataFileType:function(obj){
			var img = obj.value;
			var suffix =img.substring(img.lastIndexOf("."),img.length);
			var rep = new RegExp('(.gif|.GIF|.jpeg|.JPEG|.jpg|.JPG|.png|.PNG]$)');
			if(!rep.test(suffix)){
				$.growlUI('图片格式不正确');
				$("#uploadimg").attr('disabled','disabled');
			}else{
				$("#uploadimg").attr('disabled',false);
			}
		},
		//上传
		imgupload:function(){
			if($("input[type=file][name='file']").val() != ''){
				$("#img_upload_form").ajaxSubmit({
					url:'/CMS/main/article_uploadimage',
					type:'post',
					dataType:'json',
					success:function(data){
						if(data > 0){
							$.growlUI('上传成功');
							sourcekdialog.remove();
							EditArticle.source(1,'图片库');
						}else{
							$.growlUI(data);
						}
					},
					error:function(msg){
						$.growlUI('上传出错');
					}
				});
			}else{
				$.growlUI('请选择图片');
			}
		}
		
};
var DivTab = {
		clickOn:function(id,di){
			$("#div_tab"+id).removeClass();
			$("#div_tab"+id).addClass('div_li_style_on');
			$("#div_tab_sorce"+id).show();
			$("#div_tab"+di).removeClass();
			$("#div_tab"+di).addClass('div_li_style');
			$("#div_tab_sorce"+di).hide();
		}	
};

var VP = {
	kdialog:null,
	loadWeb : function(){
		$.ajax({
			url:'websiteinfoSource',
			type:'post',
			dataType:'json',
			success:function(data){
				var html = '<option value="" disabled="disabled" selected="selected">----站点列表----</option>';
				$.each(data,function(i,u){
					html += '<option value="'+u.id+'">'+u.ftitle+'</option>';
				});
				$("#select_web").html(html);
			},
			error:function(msg){
				$.growlUI('网站数据加载出错');
			}
		});
	},
	loadTcolunmSource : function(obj,isSelct){
		_waiting._show();
		$.ajax({
			url:'tcolunmSource',
			type:'post',
			data:{'webid':isSelct?obj.value:obj.id,'check':$("#check_state").val(),'type':0},
			dataType:'json',
			success:function(data){
				_articleCount = 0 ;
				$("#left_tree").html($.fn.simpleTree(data,'/CMS',false,isSelct?obj.options[obj.selectedIndex].text:obj.innerHTML,'VP.loadPhotoSource()'));
				$("#website_ArticleCont").html('('+_articleCount+')');
				_waiting._hide();
			},
			error:function(msg){
				$.growlUI('栏目加载出错');
				_waiting._hide();
			}
		});
	},
	loadPhotoSource : function(){
		$("#start").val(0);
		$.ajax({
			url:'videoPhotoSource',
			type:'post',
			dataType:'json',
			data:{'fcolumnid':liObj.value,'start':$("#start").val(),'limit':$("#limit").val()},
			success:function(data){
				page(data.count,$("#limit").val(),1,"pagePanel","VP.gotoPage",true,"VP.loadPhotoSource();","limit");
				VP.bindData(data);
			},
			error:function(msg){
				$.growlUI('数据加载出错');
			}
		});
	},
	gotoPage : function(nowPage){
		$("#start").val(nowPage);
		$.ajax({
			url:'videoPhotoSource',
			type:'post',
			dataType:'json',
			data:{'fcolumnid':liObj.value,'start':$("#start").val(),'limit':$("#limit").val()},
			success:function(data){
				VP.bindData(data);
			},
			error:function(msg){
				$.growlUI('数据加载出错');
			}
		});
	},
	bindData : function(data){
		$("#totalCount").html('总共找到：' + data.count + '条数据');
		var html = '';
		if(data != ''){
			var line,temp = 0,lineNum = 0,_size = data.source.length;
			var wH,hH,zoom;
			$.each(data.source,function(i,u){
				zoom = Math.max( Math.max(u.rWidth/150,1) , Math.max(u.rHeight/150,1) );
				wH   = u.rWidth / zoom;
				hH   = u.rHeight / zoom;
				
				line = '';
				if(temp == 0)
					line += '<tr>';
				
				line += '<td onclick="VP.check(this,'+u.id+')" align="center" style="padding:10px;width:20%;height:100%;"><div id="img_box_'+u.id+'"><img src="' +u.resourceUrl+ '" width="'+ wH +'px" height="' + hH + 'px"/></div>\
						<Br/><span class="span" '+(u.state == 1?"style=\"color:#ccc;\"":"")+'  title="'+u.resourceName+'" id="img_name_'+u.id+'"><input type="checkbox" name="vp_check" id="vp_check_'+u.id+'"  data="'+u.summary+'"  value="'+u.id+'"/>' +u.resourceName+ '</span>\
						</Br><span class="span"><b id="summary_'+u.id+'" '+(u.state == 1?"style=\"font-size:13px;color:#ccc;\"":"style=\"font-size:13px;color:red;\"") + ' title="' + u.summary + '">简介:'+u.summary+'<b></span>\
						' +(u.state == 1?"<font size=\"3\" color=\"#53E4F4\">[已冻结]</font>":"")+ '</td>';
				temp ++;
				
				if(temp >= 5){
					lineNum++;
					line += '</tr>';
					temp = 0;
				}
				
				html += line;
			});
			if((_size - lineNum * 5) < 5 && (_size - lineNum * 5) > 0){
				for(var j = 0;j < 5 - (_size - lineNum * 5);j ++){
					html += '<td style="width:20%;"></td>';
					temp ++;
				}
				html += '</tr>';
			}
			
		}else{
			html = '<tr><td colspan="3">无数据</td></tr>';
		}
		$("#dataGrid").html(html);
	},
	check : function(obj,id){
		if(!$("#vp_check_" + id)[0].checked){
			$("#dataGrid").find('td').removeClass().addClass('tdbg');
			$("input[type='checkbox'][name='vp_check']:checked").attr('checked',false);
			$(obj).removeClass().addClass('tdbg2');
		}else{
			$(obj).removeClass().addClass('tdbg');
		}
		$("#vp_check_" + id).attr('checked',!$("#vp_check_" + id)[0].checked);
	},
	getCheckObj : function(){
		return $("input[type='checkbox'][name='vp_check']:checked");
	},
	editeSummary : function(){
		var checkArr = this.getCheckObj();
		if(checkArr.size() > 0){
			var summary = checkArr.attr('data');
			var imgtitle = $("#img_name_" + checkArr.val()).text();
			var html = '<table style="width:100%;" height="280px" border="0"><tr><td height="30px">简介：</td><td rowspan="2"  style="text-align:center;">' +$("#img_box_" + checkArr.val()).html()+ '</td></tr>\
						<tr><td valign="top"><textarea id="photo_summary" style="width:97%;height:80%;border:1px solid black;font-size:20px;">' +summary+ '</textarea></td></tr></table>';
			this.kdialog = KindEditor.dialog({
				width : 550,
				height: 350,
				title : '图片说明 　　<b>'+ imgtitle + '<b/>',
				body : '<div style="padding:10px;height:300px;">'+html+'</div>',
				shadowMode:true,
				closeBtn : {
					name : '关闭',
					click : function(e) {
						VP.kdialog.remove();
					}
				},
				yesBtn : {
					name : '保存',
					click : function(e) {
						var summ = $("#photo_summary").val();
						if(summ != ''){
							var data = {'id':checkArr.val(),'summary':summ,'operType':2};
							VP.update(data,1);
						}
					}
				},
				noBtn : {
					name : '取消',
					click : function(e) {
						VP.kdialog.remove();
					}
				}
			});
		}else{
			$.growlUI('请选择一项进行编辑');
		}
	},
	update :function(data,S){
		$.ajax({
			url:'photoOperation',
			type:'post',
			data:data,
			dataType:'json',
			success:function(res){
				$.growlUI(res > 0?'操作成功':'操作失败');
				if(res >0 ){
					if(S){
						$("#summary_" + data.id).attr('title',data.summary).html('简介：' + data.summary);
						VP.getCheckObj().attr('data',data.summary);
						VP.kdialog.remove();
					}else{
						VP.loadPhotoSource();
					}
				}
			},
			error:function(msg){
				$.growlUI('系统出错，保存失败');
			}
		});
	},
	freeze : function(){
		var obj = this.getCheckObj();
		if(obj.size() > 0){
			var data = {'id':obj.val(),'state':1,'operType':2};
			VP.update(data);
		}else{
			$.growlUI('选项未选中');
		}
	},
	unfreeze : function(){
		var obj = this.getCheckObj();
		if(obj.size() > 0){
			var data = {'id':obj.val(),'state':0,'operType':2};
			VP.update(data);
		}else{
			$.growlUI('选项未选中');
		}
	},
	del : function(){
		var obj = this.getCheckObj();
		if(obj.size() > 0){
			var data = {'id':obj.val(),'operType':3};
			VP.update(data);
		}else{
			$.growlUI('选项未选中');
		}
	}
};



window.onload = VP.loadWeb();
